﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.IO;
using HAVNet.Core.LitJson;

namespace yiracli
{
    public class User
    {
        public string   name            { get; private set; }
        public string   emailAddress    { get; private set; }
        public string   displayName     { get; private set; }
        public bool     active          { get; private set; }
        public Dictionary<string, string> avatarUrls { get; set; }

        public User ()
        { 
            //
        }

        public static User get (Autorizacion auto, string name)
        {
            string url = string.Format(
                "http://sm-jiratest:8080/rest/api/latest/user?username={0}",
                name
            );
            HttpWebRequest req = WebRequest.Create(url) 
                as HttpWebRequest;
            req.ContentType = "application/json";
            req.Headers.Add("Authorization", "Basic " + auto.basic64);
            req.Method = "GET";
            HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
            string respuesta = resp.leer();
            JsonData jdata = JsonMapper.ToObject(respuesta);

            User user           = new User();
            user.active         = bool.Parse( jdata["active"].ToString() );
            user.avatarUrls     = null;
            user.displayName    = jdata["displayName"].ToString();
            user.emailAddress   = jdata["emailAddress"].ToString();
            user.name           = jdata["name"].ToString();

            return user;
        }
    }
}
